package com.lc1993929.mydistservers;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;

/**
 * Created by LiuChang on 2018/3/16.
 */
public class PayServiceProvider {
    private ZooKeeper zooKeeper = null;

    private void connectZooKeeper() throws IOException {
        zooKeeper = new ZooKeeper("hdp1:2181,hdp2:2181,hdp3:2181", 2000, null);
    }

    private void handleService() throws InterruptedException {
        System.out.println("服务器开始接受业务请求。。。。。。");
        Thread.sleep(Long.MAX_VALUE);
    }


    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        PayServiceProvider payServiceProvider = new PayServiceProvider();
        //    获取zookeeper客户端
        payServiceProvider.connectZooKeeper();
        //    注册信息
        Stat stat = payServiceProvider.zooKeeper.exists("/servers", false);
        if (stat == null) {
            payServiceProvider.zooKeeper.create("/servers", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }

        payServiceProvider.zooKeeper.create("/servers/server", args[0].getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        //处理业务
        payServiceProvider.handleService();
    }
}
